package com.example.leoss.zhuye.img_tongxu_phone;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leoss.M_wode.MyMusicPlayerView;
import com.example.leoss.R;
import com.example.leoss.zhuye.File_xianshi.ToFileActivity;
import com.example.leoss.zhuye.ParseXls.DBOpenHelper;

public class Tongxu_img_Activity extends AppCompatActivity implements View.OnClickListener {
    private MyMusicPlayerView musicView;
    private View wode_inmage_p1;
    TextView textView_name,textView_sex,textView_weixing,textView_qq,textView_address,textView_majar,textView_phoem;
    ToFileActivity  toFileActivity;
    Button button_inten_xiugai;
    String name;
    String phome;
    String address;
    String major;
    String qq ;
    String sex;
    private  static  int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tongxu_img_);
        
         setTitle("联系人");
         findID();//初始化
        intentes();//intent接收


        musicView = (MyMusicPlayerView)findViewById(R.id.my_music_view);
        wode_inmage_p1=findViewById(R.id.my_music_image_p1);

        wode_inmage_p1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");//选择图片
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 200);
            }
        });


        MyMusicPlayerView myMusicPlayerView=new MyMusicPlayerView(this);
        musicView.setImage1(null);
    }


    /*
    meun
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.delete,menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){

            case R.id.delete:
                DBOpenHelper dbOpenHelper=new DBOpenHelper(Tongxu_img_Activity.this,"user1.db",null,1);
                SQLiteDatabase sqLiteDatabase=dbOpenHelper.getWritableDatabase();
                String sql = "delete from u_user4 where _id = "+id;
                sqLiteDatabase.execSQL(sql);
            //  sqLiteDatabase.delete("user","_id=?",new String[]{});
                Log.e("","delete-------------------------"+id);
                Toast.makeText(Tongxu_img_Activity.this,"删除成功",Toast.LENGTH_LONG).show();
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }




    private void intentes() {

        Intent intent=getIntent();
        id=intent.getIntExtra("ID",0);
        name=intent.getStringExtra("name");
        phome= intent.getStringExtra("phome");
        address= intent.getStringExtra("address");
       major=intent.getStringExtra("major");
        qq = intent.getStringExtra("qq");
        sex=intent.getStringExtra("sex");

     //   String weixing=intent.getStringExtra("weixing");

        String[] strings=new String[]{name,phome,address,major,qq,sex};
        for (int i=0;i<strings.length;i++){
            if (TextUtils.isEmpty(strings[i])){
                strings[i]="Blank";
            }

        }
        textView_name.setText(name);
        textView_phoem.setText(phome);
        //textView_weixing.setText(weixing);
        textView_sex.setText(sex);
        textView_majar.setText(major);
        textView_address.setText(address);
        textView_qq.setText(qq);
    }

    private void findID() {
        textView_address=findViewById(R.id.tongxu_address);
        textView_majar=findViewById(R.id.tongxu_majar);
        textView_sex=findViewById(R.id.tongxu_sex);
        textView_name=findViewById(R.id.tongxu_name);
        textView_qq=findViewById(R.id.tongxu_qq);
        button_inten_xiugai=findViewById(R.id.tongxu_img_Activity_intent);
        //textView_weixing=findViewById(R.id.tongxu_weixing);
        textView_phoem=findViewById(R.id.tongxu_phome);
        button_inten_xiugai.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tongxu_img_Activity_intent:
          Intent intent=new Intent(this,XiugaiActivity.class);
                intent.putExtra("ID",id);
                intent.putExtra("name",textView_name.getText().toString());
                intent.putExtra("phome",textView_phoem.getText().toString());
                intent.putExtra("address",textView_address.getText().toString());
                intent.putExtra("major",textView_majar.getText().toString());
                intent.putExtra("qq",textView_qq.getText().toString());
                intent.putExtra("sex",textView_sex.getText().toString());
                startActivityForResult(intent,1005);break;

        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            String path;
            Uri uri = data.getData();
            if ("file".equalsIgnoreCase(uri.getScheme()))
            {//使用第三方应用打开
                path = uri.getPath();
                musicView.setImage1(path);
                Toast.makeText(this,path+"ccc1",Toast.LENGTH_SHORT).show();
                return;
            }
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
            {//4.4以后
                toFileActivity=new ToFileActivity();
                path = toFileActivity.getPath(this, uri);
                musicView.setImage1(path);
                Toast.makeText(this,path+"ccc2",Toast.LENGTH_SHORT).show();
            } else
            {//4.4以下下系统调用方法
                path = toFileActivity.getRealPathFromURI(uri);
                musicView.setImage1(path);
                Toast.makeText(this,path+"ccc3",Toast.LENGTH_SHORT).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1005){
            //判断接口是否符合，符合接受传值
            //接受第二个页面的值，然后设置给某个控件

                textView_name.setText(data.getStringExtra("name1"));
                textView_sex.setText(data.getStringExtra("sex1"));
                textView_address.setText(data.getStringExtra("address1"));
                textView_majar.setText(data.getStringExtra("major1"));
                textView_phoem.setText(data.getStringExtra("phone1"));
                textView_qq.setText(data.getStringExtra("qq1"));
        }


    }
}
